/**
 * 
 */
package cn.com.xf.web.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

/**
 * @author fangjinsong
 * 
 */
public class PageTag extends TagSupport {
	private static final long serialVersionUID = -1768927674922379810L;
	StringBuffer buffer = new StringBuffer();
	// 传入参数
	private String href;
	private String firstPageUrl;
	private String pageSize;
	private String currentPage;
	private String totalRecord;

	@Override
	public int doStartTag() throws JspException {
		try {
			initTag();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Tag.EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		setBodyHtml();
		write(buffer, pageContext.getOut());
		return super.doEndTag();
	}

	public void initTag() {

	}

	public void setBodyHtml() {

		buffer = new StringBuffer();

		if (!StringUtils.isNotBlank(totalRecord)) {
			buffer.append("无记录");
			return;
		}
		if (!StringUtils.isNotBlank(pageSize)) {
			buffer.append("无记录");
			return;
		}
		if (!StringUtils.isNotBlank(currentPage)) {
			buffer.append("无记录");
			return;
		}

		if (Integer.valueOf(totalRecord) == 0) {
			buffer.append("无记录");
			return;
		}
		if (Integer.valueOf(pageSize) == 0) {
			return;
		}
		if (Integer.valueOf(currentPage) == 0) {
			return;
		}

		int currPage = Integer.valueOf(currentPage);
		int pageCount = Integer.valueOf(totalRecord) / (Integer.valueOf(pageSize));
		int mod = Integer.valueOf(totalRecord) % (Integer.valueOf(pageSize));
		if (mod != 0) {
			pageCount++;
		}
		// 1.只有一页
		String jumpDiv = "<div class='fr'><a onclick=\"pageNumInput('" + href
				+ "')\" class='a-btn-gray-small'><span>确认</span></a></div>";
		String totalDiv = "<div class='fl'>共" + totalRecord + "条&nbsp;每页显示" + pageSize + "条记录</div>";
		StringBuffer sb = new StringBuffer();
		if (pageCount == 1) {
			sb.append("<div class='pageInfo fr'>");
			sb.append("<span class='page-start'>上一页</span>");
			sb.append("<span class='page-cur'>1</span>");
			sb.append("<span class='page-end'>下一页</span>");
			sb.append("<span class='page-skip'>共" + pageCount
					+ "页&nbsp;&nbsp;到第 <input type='text' size='3' class='num' count='" + pageCount
					+ "' id='pageNumInput'>页</span>");
			sb.append("</div>");
		}

		else if (pageCount == 2) {
			if (currPage == 1) {
				sb.append("<div class='pageInfo fr'>");
				sb.append("<span class='page-start'>上一页</span>");
				sb.append("<span class='page-cur'>1</span>");
				sb.append("<a href=\"javascript:SMT.jumpPage('2','" + href + "')\">2</a>");
				sb.append("<a href=\"javascript:SMT.jumpPage('" + (currPage + 1) + "','" + href
						+ "')\" class='page-next'>下一页</a>");
				sb.append("<span class='page-skip'>共" + pageCount + "页&nbsp;&nbsp;到第 <input count='" + pageCount
						+ "' type='text' size='3' class='num' id='pageNumInput'>页</span>");
				sb.append("</div>");
			} else if (currPage == 2) {
				sb.append("<div class='pageInfo fr'>");
				sb.append("<a class='page-prev' href=\"javascript:SMT.jumpPage('" + (currPage - 1) + "','" + href
						+ "')\">上一页</a>");
				sb.append("<a href=\"javascript:SMT.jumpPage('1','" + href + "')\">1</a>");
				sb.append("<span class='page-cur'>" + currPage + "</span>");
				sb.append("<span class='page-end'>下一页</span>");
				sb.append("<span class='page-skip'>共" + pageCount + "页&nbsp;&nbsp;到第 <input count='" + pageCount
						+ "' type='text' size='3' class='num' id='pageNumInput'>页</span>");
				sb.append("</div>");
			}
		} else if (pageCount > 2) {
			if (currPage == 1) {
				sb.append("<div class='pageInfo fr'>");
				sb.append("<span class='page-start'>上一页</span>");
				sb.append("<span class='page-cur'>1</span>");
				for (int i = 2; i < ((5 <= pageCount) ? 6 : (pageCount + 1)); i++) {
					sb.append("<a href=\"javascript:SMT.jumpPage('" + i + "','" + href + "')\">" + i + "</a>");
				}
				if (pageCount > 5) {
					sb.append("<span class='page-break'>...</span>");
				}
				sb.append("<a href=\"javascript:SMT.jumpPage('2','" + href + "')\" class='page-next'>下一页</a>");
				sb.append("<span class='page-skip'>共" + pageCount + "页&nbsp;&nbsp;到第 <input count='" + pageCount
						+ "' type='text' size='3' class='num' id='pageNumInput'>页</span>");
				sb.append("</div>");
			} else if (currPage == pageCount) {
				sb.append("<div class='pageInfo fr'>");
				sb.append("<a class='page-prev' href=\"javascript:SMT.jumpPage('" + (currPage - 1) + "','" + href
						+ "')\">上一页</a>");
				if (pageCount - 4 > 1) {
					sb.append("<span class='page-break'>...</span>");
				}
				for (int i = (((pageCount - 4) > 0 ? (pageCount - 4) : 1)); i < pageCount; i++) {
					sb.append("<a href=\"javascript:SMT.jumpPage('" + i + "','" + href + "')\">" + i + "</a>");
				}
				sb.append("<span class='page-cur'>" + pageCount + "</span>");
				sb.append("<span class='page-end'>下一页</span>");
				sb.append("<span class='page-skip'>共" + pageCount + "页&nbsp;&nbsp;到第 <input count='" + pageCount
						+ "' type='text' size='3' class='num' id='pageNumInput'>页</span>");
				sb.append("</div>");
			} else if (currPage < pageCount && currPage > 1) {
				sb.append("<div class='pageInfo fr'>");
				sb.append("<a class='page-prev' href=\"javascript:SMT.jumpPage('" + (currPage - 1) + "','" + href
						+ "')\">上一页</a>");
				if (currPage < 5) {
					for (int i = 1; i < currPage; i++) {
						sb.append("<a href=\"javascript:SMT.jumpPage('" + i + "','" + href + "')\">" + i + "</a>");
					}
					sb.append("<span class='page-cur'>" + currPage + "</span>");
					for (int i = (currPage + 1); i < ((5 <= pageCount) ? 6 : (pageCount + 1)); i++) {
						sb.append("<a href=\"javascript:SMT.jumpPage('" + i + "','" + href + "')\">" + i + "</a>");
					}
					if (pageCount > 5) {
						sb.append("<span class='page-break'>...</span>");
					}
				} else if (currPage > (pageCount - 5 + 2)) {
					sb.append("<span class='page-break'>...</span>");
					for (int i = (pageCount - 4); i < currPage; i++) {
						sb.append("<a href=\"javascript:SMT.jumpPage('" + i + "','" + href + "')\">" + i + "</a>");
					}
					sb.append("<span class='page-cur'>" + currPage + "</span>");
					for (int i = (currPage + 1); i <= pageCount; i++) {
						sb.append("<a href=\"javascript:SMT.jumpPage('" + i + "','" + href + "')\">" + i + "</a>");
					}
				} else if (currPage < (pageCount - 2) && currPage > (4)) {
					sb.append("<span class='page-break'>...</span>");
					for (int i = (currPage - 2); i < currPage; i++) {
						sb.append("<a href=\"javascript:SMT.jumpPage('" + i + "','" + href + "')\">" + i + "</a>");
					}
					sb.append("<span class='page-cur'>" + currPage + "</span>");
					for (int i = (currPage + 1); i <= (currPage + 2); i++) {
						sb.append("<a href=\"javascript:SMT.jumpPage('" + i + "','" + href + "')\">" + i + "</a>");
					}
					sb.append("<span class='page-break'>...</span>");
				}

				sb.append("<a href=\"javascript:SMT.jumpPage('" + (currPage + 1) + "','" + href
						+ "')\" class='page-next'>下一页</a>");
				sb.append("<span class='page-skip'>共" + pageCount + "页&nbsp;&nbsp;到第 <input count='" + pageCount
						+ "' type='text' size='3' class='num' id='pageNumInput'>页</span>");
				sb.append("</div>");
			}
		}
		buffer.append(jumpDiv).append(sb).append(totalDiv);
	}

	public void write(StringBuffer buffer, JspWriter out) {
		try {
			out.write(buffer.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public StringBuffer getBuffer() {
		return buffer;
	}

	public void setBuffer(StringBuffer buffer) {
		this.buffer = buffer;
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

	public String getFirstPageUrl() {
		return firstPageUrl;
	}

	public void setFirstPageUrl(String firstPageUrl) {
		this.firstPageUrl = firstPageUrl;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public String getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(String totalRecord) {
		this.totalRecord = totalRecord;
	}

}
